package com.xinxin.leetcode.problem606;

import com.xinxin.leetcode.common.CommonUtil;
import com.xinxin.leetcode.common.TreeNode;
import com.xinxin.leetcode.common.TreeUtil;

/**
 * @author ShiXinXin
 * @date 2020-02-10 22:08
 */
class Solution {
    public String tree2str(TreeNode t) {
        if (t == null) {
            return "";
        }
        if (t.left == null && t.right == null) {
            return String.valueOf(t.val);
        }
        return t.val + (t.left != null ? "(" + tree2str(t.left) + ")" : "()") + (t.right != null ? "(" + tree2str(t.right) + ")" : "");
    }

    public static void main(String[] args) {
        TreeNode root = TreeUtil.createTree(CommonUtil.createArr(1, 2, 0, 4, 0, 0, 3, 0, 0));
        System.out.println(new Solution().tree2str(root));
    }
}
